package com.java.shop.order.web;

import com.java.shop.order.service.OrderService;
import com.java.shop.order.vo.OrderConfirmVo;
import com.java.shop.order.vo.OrderSubmitRespVo;
import com.java.shop.order.vo.OrderSubmitVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;

import java.util.concurrent.ExecutionException;


@Controller
public class OrderWebController {
    @Autowired
    OrderService orderService;

    @GetMapping("/confirm.html")
    public String confirm(Model model) throws ExecutionException, InterruptedException {
        OrderConfirmVo vo = orderService.getConfirmInfo();
        model.addAttribute("orderConfirm",vo);
        return "confirm";
    }

    @PostMapping("/submitOrder")
    public String submitOrder(OrderSubmitVo vo,Model model){
        try {
            OrderSubmitRespVo respVo = orderService.submitOrder(vo);
            if(respVo.getCode() == 0){
                model.addAttribute("submitOrderResp",respVo.getOrder());
                return "pay";
            }else{
                switch (respVo.getCode()){
                    case 1:
                        System.out.println("令牌验证错误");
                        break;
                    case 2:
                        System.out.println("验价未通过");
                        break;
                    case 3:
                        System.out.println("锁定库存失败");
                        break;
                }
                return "redirect:/confirm.html";
            }
        } catch (Exception e) {
            return "redirect:/confirm.html";
        }
    }

}
